package ch.ti.kmltomap;

import org.xmlpull.v1.XmlPullParser;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.MarkerOptions;


public class Point extends Geometry{
	
	public static final String ALTITUDE_MODE_CLAMP_TO_GROUND = "clampToGround";
	public static final String ALTITUDE_MODE_RELATIVE_TO_GROUND = "relativeToGround";
	public static final String ALTITUDE_MODE_ABSOLUTE = "absolute";
	
	public boolean extrude = false;
	public String altitudeMode = ALTITUDE_MODE_CLAMP_TO_GROUND;
	
	private Coordinates coordinates = null;
	
	public Point(){}
	
	public Point(XmlPullParser xpp){
		try{
			this.id = xpp.getAttributeValue(null, "id");
			int eventType = xpp.next();
			String tagName = "";
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if(eventType == XmlPullParser.START_TAG){
					tagName = xpp.getName();
				}
				if(eventType == XmlPullParser.TEXT){
					if(tagName.equals("coordinates")){
						this.addCoordinatesString(xpp.getText().trim());
					}else if(tagName.equals("extrude")){
						this.extrude = xpp.getText().trim().equals("1");
					}
				}else if(eventType == XmlPullParser.END_TAG){
					if(xpp.getName().equals("Point")){
						break;
					}
				}
				eventType = xpp.next();
			}
		}catch (Exception e) {
		}		
	}
	
	public void addCoordinatesString(String coordinates){
		if(this.coordinates == null)
			this.coordinates = new Coordinates(coordinates);
	}
	
	public Coordinates coordinates(){
		return this.coordinates;
	}
	
	public void draw(GoogleMap map){
		this.draw(map,"");
	}
	
	public void draw(GoogleMap map, String testo){
		for (Coordinate coordinate : this.coordinates.coordinates()) {
			MarkerOptions mo = new MarkerOptions();
			mo.position(coordinate.latLng()).icon(BitmapDescriptorFactory.defaultMarker(
				     BitmapDescriptorFactory.HUE_AZURE));
			if(testo != null && !testo.equals(""))
				mo.title(testo);
			map.addMarker(mo);
		}
	}
	
}
